package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.mapper.AccountMapper;
import com.xmg.p2p.base.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.service.impl
 * @author: Andy
 * @date: 2020.12.2 16:15
 * @Description: AccountService的实现类
 * @since JDK 1.8
 */
@Service
public class AccountServiceimpl implements AccountService {
    private AccountMapper accountMapper;
    @Autowired
    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    public void update(Account account) {
        int i = this.accountMapper.updateByPrimaryKey(account);
        if (i == 0){
            throw new RuntimeException("乐观锁失败,"+account.getId());
        }
    }

    public void add(Account account) {
        int i = accountMapper.insert(account);
        if (i == 0){
            throw new RuntimeException("乐观锁失败,"+account.getId());
        }
    }

    public Account getById(Long id) {
        // 获取当前登录的Account(用户对应的账户信息)
        Account account = this.accountMapper.selectByPrimaryKey(id);
        if (!account.getIsValidate()){  // 判断当前账户是否可以校验通过，如果不通过 进入if
            throw new RuntimeException("账户"+id+"可能被恶意篡改");
        }
        return account;
    }
}
